// pages/detail/index.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cur_time: new Date().getTime().toString().slice(0, 10),    
    courseData: '',
    is_lastday:false,
    is_ban:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {    
    wx.showLoading({
      title: '加载中',
    })    
    
    this.setData({
        is_lastday: this.formatDate(new Date()) == this.formatDate(this.getCurrentMonthLast())
    })
    if(this.data.is_lastday == true){
      wx.showModal({
        title: '提示',
        content: '系统维护中。。。',
        showCancel: false,
        success(res) {
        }
      })
    }
    
    //获取热门课程
    wx.request({
      url: 'https://sng.tsletu.com/api/wxapp/course/info',
      data:{
        id: options.id
      },
      header: {
        'XX-Device-Type': 'wxapp',
        'XX-Token': app.globalData.token
      },
      success: res => {  
        this.setData({
          courseData: res.data.data
        })          
      }
    })  
    
    if (app.globalData.userInfo){
      //获取学员
    wx.request({
      url: 'https://sng.tsletu.com/api/wxapp/user/getuserstudents',
      header: {
        'XX-Device-Type': 'wxapp',
        'XX-Token': app.globalData.token
      },
      success: res => {
        let stus = res.data.data
        stus.forEach((item)=>{
          if (item.name == "黄奕翔" || item.phone == "15917332646"){
            this.setData({
              is_ban:true
            })
          }
        })
      }
    })
    }
    
    
    wx.hideLoading()
    
  },
  toOrder:function(data){ 
    if (!app.globalData.userInfo) {
      wx.showModal({
        title: '提示',
        content: '你还没登录，请登录后再报名！',
        success(res) {
          if (res.confirm) {
            app.check();
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      }) 
      return;     
    }
    wx.navigateTo({
      url: '/pages/seat/index?course_id=' + data.currentTarget.dataset.course_id
    })
  },
  getCurrentMonthLast:function(){
    var date = new Date();
    var currentMonth = date.getMonth();
    var nextMonth = ++currentMonth;
    var nextMonthFirstDay = new Date(date.getFullYear(), nextMonth, 1);
    var oneDay = 1000 * 60 * 60 * 24;
    return new Date(nextMonthFirstDay - 1);
  },
  formatDate:function(date){
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    var d = date.getDate();
    m = m < 10 ? "0" + m : m;
    d = d < 10 ? "0" + d : d;
    return y + "-" + m + "-" + d;
  }
})